package com.seveneight.baijiesteward.ui.main.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.base.BaseActivity;
import com.seveneight.baijiesteward.base.MessageBean;
import com.seveneight.baijiesteward.bean.AnyBean;
import com.seveneight.baijiesteward.bean.BranchBean;
import com.seveneight.baijiesteward.bean.CreditBean;
import com.seveneight.baijiesteward.http.HttpConfig;
import com.seveneight.baijiesteward.http.MyCallBack;
import com.seveneight.baijiesteward.ui.user.model.UpdateCardDateModel;
import com.seveneight.baijiesteward.views.BranchBankListDialog;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 设置卡还款日期
 */
public class UpdateCreditCardDateActivity extends BaseActivity implements View.OnClickListener{
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.user_name)
    TextView user_name;
    @BindView(R.id.card_number)
    TextView card_number;
    @BindView(R.id.bill_date)
    TextView bill_date;
    @BindView(R.id.pay_date)
    TextView pay_date;

    CreditBean cardBean;
    int index;
    private BranchBankListDialog branchBankListDialog;
    BranchBean dateBean;
    private List<BranchBean> dateData=new ArrayList<>();
    UpdateCardDateModel api=new UpdateCardDateModel();
    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_update_credit_card_date);
        cardBean= (CreditBean) getIntent().getSerializableExtra("cardBean");
        index=getIntent().getIntExtra("index",1);
    }

    @Override
    protected void initView() {
        tv_title.setText("修改信用卡");
        user_name.setText(cardBean.getRealName());
        card_number.setText("**** **** **** "+cardBean.getCreditCardNo().substring(cardBean.getCreditCardNo().length()-4,cardBean.getCreditCardNo().length()));
        bill_date.setText("每月"+cardBean.getBillDate()+"号");
        pay_date.setText("每月"+cardBean.getRepayDate()+"号");
    }

    @Override
    protected void initData() {
        addDate();
    }

    public void addDate(){
        for (int i=1;i<29;i++){
            BranchBean bean=new BranchBean();
            bean.setCode(i+"");
            bean.setKey(i+"");
            bean.setText("每月"+i+"号");
            dateData.add(bean);
        }
    }
    @Override
    protected void setListener() {

    }
    @Subscribe
    public void onEvent(MessageBean messageBean){
        switch (messageBean.getType()){
            case 11:
                dateBean = (BranchBean) messageBean.getData();
                cardBean.setBillDate(dateBean.getKey());
                bill_date.setText("每月"+cardBean.getBillDate()+"号");
                break;
            case 12:
                dateBean = (BranchBean) messageBean.getData();
                cardBean.setRepayDate(dateBean.getKey());
                pay_date.setText("每月"+cardBean.getRepayDate()+"号");
                break;
        }
    }

    @OnClick({R.id.iv_back,R.id.bill_date_layout,R.id.bill_date,R.id.pay_date_layout,R.id.bt_next,R.id.pay_date})
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.bill_date_layout:
                branchBankListDialog = new BranchBankListDialog(activity, 11, dateData);
                branchBankListDialog.show();
                break;
            case R.id.bill_date:
                branchBankListDialog = new BranchBankListDialog(activity, 11, dateData);
                branchBankListDialog.show();
                break;
            case R.id.pay_date_layout:
                branchBankListDialog = new BranchBankListDialog(activity, 12, dateData);
                branchBankListDialog.show();
                break;
            case R.id.pay_date:
                branchBankListDialog = new BranchBankListDialog(activity, 12, dateData);
                branchBankListDialog.show();
                break;
            case R.id.bt_next:
//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
                        api.getBankList(getContext(), jsonFormat.updateCardDate(cardBean.getBillDate(),cardBean.getRepayDate(),cardBean.getCreditCardID()), new MyCallBack<AnyBean>() {
                            @Override
                            public void onSuccess(AnyBean anyBean) {
                                Intent intent=new Intent();
                                intent.putExtra("cardBean",cardBean);
                                intent.putExtra("index",index);
                                setResult(RESULT_OK,intent);
                                Toast.makeText(getContext(),"修改成功",Toast.LENGTH_SHORT).show();
                                finish();
                            }

                            @Override
                            public void onFailed() {

                            }
                        });
//                    }
//                }).start();

                break;
        }
    }
}
